'use strict';

const { Controller } = require('ee-core');
const Log = require('ee-core/log');
const Services = require('ee-core/services');
const Game = require("../database/model/game")
const FinanceBo = require('../bo/finance-bo')
const fs = require('fs').promises;
const UserMapper = require("../database/mapper/user");
const UserRecordMapper = require("../database/mapper/user-record");

/**
 * example
 * @class
 */
class ExampleController extends Controller {

  constructor(ctx) {
    super(ctx);
  }


  /**
   * 所有方法接收两个参数
   * @param args 前端传的参数
   * @param event - ipc通信时才有值。详情见：控制器文档
   */

  /**
   * test
   */
  async test () {

    let obj = {
      gameId: 6150
      ,serverId :499
      ,roleId :8454643
      ,userName : "ca82121370"
      ,deviceinfo : '{"network":"wifi","screen_resolution":"1080x1920","system":"9","electric":0,"processor_model":"ARMv8 processor rev 1 (aarch64)","baseband":"","model":"M973Q","oaid":"null_gaore_null","adinfo":"","ad_device":"","user-agent":"Mozilla/5.0 (Linux; Android 9; M973Q Build/PQ3A.190705.11271837; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/91.0.4472.114 Mobile Safari/537.36; SHGR GaoreGame/2.3.8","ipua_md5":""}'
      ,clientIp : "233.39.224.87"
    }

    let bo = new FinanceBo()

    return await bo.getBalance(obj)

  }

  async db(){
     const game = await Game.create({
        name:'好浍以会'
      })

    return game.id
  }

  async saveUserInfo(){

    let userMapper = new UserMapper()
    let list = await userMapper.getList({},false)
    let path = "F:\\py-code\\red\\runtime\\"
    for(let user of list){
      let loginFile = path + user.username + "\\login.txt"
      let roleFile = path + user.username + "\\user.txt"
      let cookieFile = path + user.username + "\\cookie.txt"
      let deviceFile = path + user.username + "\\device.txt"

      user.login_info = await fs.readFile(loginFile, 'utf8');

      user.role_info = await fs.readFile(roleFile, 'utf8');

      user.cookies = await fs.readFile(cookieFile, 'utf8');

      user.device = await fs.readFile(deviceFile, 'utf8');

      await user.save()

    }


  }


}

ExampleController.toString = () => '[class ExampleController]';
module.exports = ExampleController;  